package color.koitq.picker.fragment;

import android.content.ContentValues;
import android.content.Intent;
import android.text.TextUtils;

import androidx.activity.result.ActivityResultLauncher;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.quexin.pickmedialib.PickerMediaParameter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import color.koitq.picker.R;
import color.koitq.picker.activty.HomeColorListActivity;
import color.koitq.picker.ad.AdFragment;
import color.koitq.picker.adapter.Tab4Adapter;
import color.koitq.picker.decoration.GridSpaceItemDecoration;
import color.koitq.picker.entity.PaletteModel;
import color.koitq.picker.entity.RefreshEvent;
import color.koitq.picker.entity.Tab3Model;
import color.koitq.picker.entity.Tab4Model;
import color.koitq.picker.util.SQLdm;
import color.koitq.picker.util.SharedPreferencesUtils;
import color.koitq.picker.util.ThisUtils;

public class Tab4Fragment extends AdFragment {

    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.topbar)
    QMUITopBarLayout topbar;
    private Tab4Adapter mAdapter;

    private ActivityResultLauncher<PickerMediaParameter> pickerMeida;
    private SharedPreferencesUtils mFlag;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab4;
    }

    @Override
    protected void init() {
        topbar.setTitle("集色卡");
        initList();

    }

    private void initList() {
        list.setLayoutManager(new GridLayoutManager(getContext(), 1));
        list.addItemDecoration(new GridSpaceItemDecoration(1, QMUIDisplayHelper.dp2px(getContext(), 24),
                QMUIDisplayHelper.dp2px(getContext(), 0)));
        mAdapter = new Tab4Adapter();
        list.setAdapter(mAdapter);
        mAdapter.setClickListener(new Tab4Adapter.Tab4ItemClickListener() {
            @Override
            public void click(Tab4Model model) {
                Intent intent = new Intent(getContext(), HomeColorListActivity.class);
                intent.putExtra("tab4Model", model);
                startActivity(intent);
            }

            @Override
            public void delete(Tab4Model model) {
                if (model.modelFlag == ThisUtils.NOMODEL) {
                    if (model.modelId == 1) {
                        mFlag.putValue("seka1", false);
                    } else if (model.modelId == 2) {
                        mFlag.putValue("seka2", false);
                    }
                } else if (model.modelFlag == ThisUtils.PALETTERMODEL) {
                    SQLdm.setFavorite(0, model.modelId);
                } else {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("collection", 0);
                    LitePal.update(Tab3Model.class, contentValues, model.modelId);
                }
                loadData();
            }
        });
        loadData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updata(RefreshEvent event) {
        if (event != null) {
            loadData();
        }
    }

    private void loadData() {
        new Thread(() -> {
            List<Tab3Model> tab3Models = LitePal.order("id desc").where("collection=?", 1 + "").find(Tab3Model.class);
            ArrayList<PaletteModel> paletteModels = SQLdm.queryFavoriteColors();
            List<Tab4Model> models = new ArrayList<>();
            //开始预留两个色卡
            mFlag = new SharedPreferencesUtils(getContext(), "flag");
            if (mFlag.getValue("seka1", true)) models.addAll(ThisUtils.getSeka1());
            if (mFlag.getValue("seka2", true)) models.addAll(ThisUtils.getSeka2());

            for (Tab3Model tab3Model : tab3Models) {
                Tab4Model model = new Tab4Model();
                List<String> colors = new ArrayList<>();
                colors.add(tab3Model.color1);
                colors.add(tab3Model.color2);
                colors.add(tab3Model.color3);
                colors.add(tab3Model.color4);
                colors.add(tab3Model.color5);
                model.colors = colors;
                model.modelFlag = ThisUtils.TAB3MODEL;
                model.modelId = (int) tab3Model.id;
                models.add(model);
            }
            for (PaletteModel paletteModel : paletteModels) {
                Tab4Model model = new Tab4Model();
                String colors = paletteModel.getColors();
                if (!TextUtils.isEmpty(colors) && colors.contains(",")) {
                    String[] split = colors.split(",");
                    model.colors = Arrays.asList(split);
                    model.title = paletteModel.getTitle();
                    model.modelFlag = ThisUtils.PALETTERMODEL;
                    model.modelId = paletteModel.getPaletteId();
                    models.add(model);
                }
            }
            requireActivity().runOnUiThread(() -> requireActivity().runOnUiThread(() -> mAdapter.setNewInstance(models)));
        }).start();



    }

    @Override
    protected void fragmentAdClose() {

    }
}
